/*
 * Created by JFormDesigner on Sat May 22 21:20:38 CST 2021
 */

package com.dfrz.bookstore.view;

import com.dfrz.bookstore.entry.User;
import com.dfrz.bookstore.service.UserService;
import com.dfrz.bookstore.service.impl.UserServiceImpl;
import com.dfrz.bookstore.util.CommenUtil;
import com.dfrz.bookstore.util.OssTest;
import com.dfrz.bookstore.util.RegularUtiles;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import javax.swing.*;

/**
 * @author Brainrain
 */
public class UserRegistration extends JFrame {
    UserServiceImpl userService = new UserServiceImpl();
    public static void main(String[] args) {
        UserRegistration userRegistration = new UserRegistration();
        userRegistration.setVisible(true);
    }
    User user = null;
    public UserRegistration(User user) {
        initComponents();
        this.user = user;
        setTitle("修改信息");
        titleLae.setText("修改信息");
        registered.setText("修改");
        loginName.setText(user.getLoginName());
        name.setText(user.getName());
        passWord.setText(user.getPassWord());
        sex.setText(user.getSex());
        email.setText(user.getEmail());
        tel.setText(user.getTel());
        address.setText(user.getAddress());
        URL url = null;
        try {
            url = new URL(user.getUserImage());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        ImageIcon icon = new ImageIcon(url);
        icon.setImage(icon.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT));
        user_Image.setIcon(icon);
    }

    public UserRegistration() {
        initComponents();
        setTitle("用户注册");
        titleLae.setText("用户注册");
    }

    /**
     * 用户选择本地图书头像
     * @param e
     */
    private void selectImageActionPerformed(ActionEvent e) {

        JFileChooser fd = new JFileChooser();
        fd.setFileSelectionMode(JFileChooser.OPEN_DIALOG);
        fd.showOpenDialog(null);
        File f = fd.getSelectedFile();
        if (f==null){
            return;
        }
        userImage.setText(f.getPath());
        ImageIcon icon = new ImageIcon(f.getPath());
        icon.setImage(icon.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT));
        user_Image.setIcon(icon);
    }
    private void registeredActionPerformed(ActionEvent e) {
        if (user!=null){
            //更新用户信息
            User user1 = new User();
            user1.setId(user.getId());
            user1.setLoginName(loginName.getText());
            //1.看用户是否改过登录名
            //2.检查修改的登录名是否重复
            if (user.getLoginName()!=name.getText()){
                user1.setLoginName(loginName.getText());
                List<User> userByUser = userService.findUserByUser(user1);
                if (userByUser.size()>0){
                    JOptionPane.showMessageDialog(null,"用户名重复");
                    label10.setText("用户名重复");
                    return;
                }

            }
            user1.setPassWord(passWord.getText());
            user1.setName(name.getText());
            user1.setSex(sex.getText());
            String emailStr = email.getText();
            //用户账户更新也要校验
            if (!RegularUtiles.regEmail(emailStr)){
                return;
            }
            //用户账户更新也要校验
            user1.setEmail(emailStr);
            if (!RegularUtiles.regPhone(tel.getText())){
                return;
            }
            user1.setTel(tel.getText());
            user1.setAddress(address.getText());
            //如果用户上传头像 即上传头像至OSS
            String userImageUrl = null;

            if (!"".equals(userImage.getText())){
                userImageUrl  = OssTest.sendToOss(userImage.getText());
                user1.setUserImage(userImageUrl); //把返回的url存入对象中
            }
            userService.updateUser(user1);
            dispose();
        }else {
            //注册用户信息
            User user1 = new User();
            user1.setLoginName(loginName.getText());
            //先检查一下登录名是否重复
            List<User> userByUser = userService.findUserByUser(user1);
            if (userByUser.size()>0){
                JOptionPane.showMessageDialog(null,"用户名重复");
                label10.setText("用户名重复");
                return;
            }
            user1.setPassWord(passWord.getText());
            user1.setName(name.getText());
            user1.setSex(sex.getText());
            if (!RegularUtiles.regEmail(email.getText())){
                return;//校检邮箱
            }
            user1.setEmail(email.getText());
            if (!RegularUtiles.regPhone(tel.getText())) {
                return;//校检电话号码
            }
            user1.setTel(tel.getText());
            user1.setAddress(address.getText());
            //如果用户上传头像 即上传头像至OSS
            String userImageUrl = null;
            if (userImage.getText()!=null){
                userImageUrl  = OssTest.sendToOss(userImage.getText());
            }else {//默认头像
                userImageUrl = "http://keyantong.oss-cn-hangzhou.aliyuncs.com/%E5%A4%B4%E5%83%8F.jpeg?Expires=1623464137&OSSAccessKeyId=LTAI5tJfScc8WuRUfZVvroVK&Signature=wMXPN%2F%2B0f6pFxo1cVdFRJKJyb8I%3D";
            }
            user1.setUserImage(userImageUrl); //把返回的url存入对象中
            //调用增加用户servlet
            boolean flag = userService.insertUser(user1);

            if (flag){
                //注册成功 关闭注册界面 打开登录界面
                dispose();
                Login login = new Login();
                login.setVisible(true);
            }

        }

    }

    private void userImageActionPerformed(ActionEvent e) {
        // TODO add your code here
    }

    private void comeBackActionPerformed(ActionEvent e) {
        //关闭登录界面
        dispose();
    }

    private void initComponents() {


        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        label1 = new JLabel();
        titleLae = new JLabel();
        name = new JTextField();
        label3 = new JLabel();
        loginName = new JTextField();
        label4 = new JLabel();
        passWord = new JTextField();
        label5 = new JLabel();
        sex = new JTextField();
        label6 = new JLabel();
        email = new JTextField();
        label7 = new JLabel();
        tel = new JTextField();
        label8 = new JLabel();
        address = new JTextField();
        label9 = new JLabel();
        userImage = new JTextField();
        registered = new JButton();
        comeBack = new JButton();
        selectImage = new JButton();
        label10 = new JLabel();
        label11 = new JLabel();
        label12 = new JLabel();
        label13 = new JLabel();
        label14 = new JLabel();
        label15 = new JLabel();
        user_Image = new JLabel();

        //======== this ========
        setTitle("\u7528\u6237\u6ce8\u518c");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setBackground(Color.lightGray);
            panel1.setLayout(null);

            //---- label1 ----
            label1.setText("\u59d3\u540d:");
            label1.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 21));
            panel1.add(label1);
            label1.setBounds(90, 140, 65, 50);

            //---- titleLae ----
            titleLae.setText("\u7528\u6237\u6ce8\u518c");
            titleLae.setEnabled(false);
            titleLae.setFont(new Font("\u9ed1\u4f53", Font.BOLD, 20));
            titleLae.setHorizontalAlignment(SwingConstants.CENTER);
            titleLae.setBackground(new Color(204, 204, 255));
            panel1.add(titleLae);
            titleLae.setBounds(215, 30, 240, 27);
            panel1.add(name);
            name.setBounds(160, 150, 95, 29);

            //---- label3 ----
            label3.setText("\u767b\u5f55\u540d:");
            label3.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 21));
            panel1.add(label3);
            label3.setBounds(80, 85, 75, 50);
            panel1.add(loginName);
            loginName.setBounds(160, 95, 95, 29);

            //---- label4 ----
            label4.setText("\u5bc6\u7801:");
            label4.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 21));
            panel1.add(label4);
            label4.setBounds(90, 190, 65, 50);
            panel1.add(passWord);
            passWord.setBounds(160, 200, 95, 29);

            //---- label5 ----
            label5.setText("\u6027\u522b:");
            label5.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 21));
            panel1.add(label5);
            label5.setBounds(90, 245, 65, 50);
            panel1.add(sex);
            sex.setBounds(160, 255, 95, 29);

            //---- label6 ----
            label6.setText("\u90ae\u7bb1:");
            label6.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 21));
            panel1.add(label6);
            label6.setBounds(90, 300, 65, 50);
            panel1.add(email);
            email.setBounds(160, 310, 95, 29);

            //---- label7 ----
            label7.setText("\u7535\u8bdd\u53f7\u7801:");
            label7.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 21));
            panel1.add(label7);
            label7.setBounds(400, 90, 110, 50);
            panel1.add(tel);
            tel.setBounds(510, 105, 95, 29);

            //---- label8 ----
            label8.setText("\u6536\u8d27\u5730\u5740:");
            label8.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 21));
            panel1.add(label8);
            label8.setBounds(400, 145, 110, 50);
            panel1.add(address);
            address.setBounds(510, 155, 95, 29);

            //---- label9 ----
            label9.setText("\u7528\u6237\u5934\u50cf:");
            label9.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 21));
            panel1.add(label9);
            label9.setBounds(365, 290, 110, 50);
            panel1.add(userImage);
            userImage.setBounds(335, 345, 145, 29);

            //---- registered ----
            registered.setText("\u6ce8\u518c");
            registered.setFont(registered.getFont().deriveFont(registered.getFont().getSize() + 20f));
            registered.addActionListener(e -> registeredActionPerformed(e));
            panel1.add(registered);
            registered.setBounds(510, 365, 155, 45);

            //---- comeBack ----
            comeBack.setText("\u8fd4\u56de");
            comeBack.setFont(comeBack.getFont().deriveFont(comeBack.getFont().getSize() + 4f));
            comeBack.addActionListener(e -> comeBackActionPerformed(e));
            panel1.add(comeBack);
            comeBack.setBounds(15, 10, 90, 35);

            //---- selectImage ----
            selectImage.setText("\u6d4f\u89c8");
            selectImage.setIcon(UIManager.getIcon("FileChooser.newFolderIcon"));
            selectImage.addActionListener(e -> selectImageActionPerformed(e));
            panel1.add(selectImage);
            selectImage.setBounds(570, 305, 50, selectImage.getPreferredSize().height);

            //---- label10 ----
            label10.setText("*");
            label10.setForeground(new Color(204, 0, 0));
            panel1.add(label10);
            label10.setBounds(265, 100, 110, label10.getPreferredSize().height);

            //---- label11 ----
            label11.setText("*");
            panel1.add(label11);
            label11.setBounds(265, 155, 5, 17);

            //---- label12 ----
            label12.setText("*");
            panel1.add(label12);
            label12.setBounds(265, 205, 5, 17);

            //---- label13 ----
            label13.setText("*");
            panel1.add(label13);
            label13.setBounds(615, 160, 5, 17);

            //---- label14 ----
            label14.setText("*");
            panel1.add(label14);
            label14.setBounds(new Rectangle(new Point(616, 108), label14.getPreferredSize()));

            //---- label15 ----
            label15.setText("* \u4e3a\u5fc5\u586b\u9879");
            panel1.add(label15);
            label15.setBounds(new Rectangle(new Point(555, 20), label15.getPreferredSize()));
            panel1.add(user_Image);
            user_Image.setBounds(465, 290, 50, 50);

            { // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(5, 5, 685, 425);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JLabel label1;
    private JLabel titleLae;
    private JTextField name;
    private JLabel label3;
    private JTextField loginName;
    private JLabel label4;
    private JTextField passWord;
    private JLabel label5;
    private JTextField sex;
    private JLabel label6;
    private JTextField email;
    private JLabel label7;
    private JTextField tel;
    private JLabel label8;
    private JTextField address;
    private JLabel label9;
    private JTextField userImage;
    private JButton registered;
    private JButton comeBack;
    private JButton selectImage;
    private JLabel label10;
    private JLabel label11;
    private JLabel label12;
    private JLabel label13;
    private JLabel label14;
    private JLabel label15;
    private JLabel user_Image;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
